const app = getApp()
const http = require('../../utils/http.js')
const util = require('../../utils/util.js')

var date = new Date()
var years = []
var months = []
var days = []

for (let i = date.getFullYear(); i <= date.getFullYear() + 5; i++) {
    years.push(i)
}
for (let i = 1; i <= 12; i++) {
    months.push(util.formatNumber(i))
}
for (let i = 1; i <= 31; i++) {
    days.push(util.formatNumber(i))
}

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: http.imgUrl,
        nav: '1',
        chooseTime: false,
        chooseTimeEnd: false,
        years: years,
        months: months,
        days: days,
        years1: years,
        months1: months,
        days1: days,
        value: [0, date.getMonth(), date.getDate() - 1],
        sValue: [0, date.getMonth()-1, date.getDate()-1],
        eValue: [0, date.getMonth(), date.getDate()-1],
        dataList: []
    },

    /**
     * 切换导航
     */
    changeNav(e) {
        let id = http.dataIndex(e)[1]
        this.setData({
            nav: id
        })
        this.getShop(id, 0)
    },

    /**
     * 到详情
     */
    goDataDetail(e) {
        let shop_id = http.dataIndex(e)[1],
            type_id = this.data.nav,
            sVal = this.data.sValue,
            eVal = this.data.eValue,
            years = this.data.years,
            months = this.data.months,
            days = this.data.days,
            sdate = years[sVal[0]] + '-' + months[sVal[1]] + '-' + days[sVal[2]],
            edate = years[eVal[0]] + '-' + months[eVal[1]] + '-' + days[eVal[2]]

        wx.navigateTo({
            url: '/pages/data/dataDetail/dataDetail?shop_id=' + shop_id + '&type_id=' + type_id + '&sdate=' + sdate + '&edate=' + edate,
        })
    },

    /**
     * 开启选择开始时间
     */
    openChooseTime() {
        this.setData({
            chooseTime: true,
            years: years,
            months: months,
            days: days,
            value: [0, date.getMonth(), date.getDate() - 1],
            sValue: [0, date.getMonth(), date.getDate() - 1],
            eValue: [0, date.getMonth(), date.getDate() - 1]
        })
    },

    /**
     * 关闭选择时间
     */
    closeChooseTime() {
        this.setData({
            chooseTime: false,
            chooseTimeEnd: false,
            value: [0, date.getMonth(), date.getDate() - 1]
        })
    },

    /**
     * 开始时间确定
     */
    confirmStart() {
        let that = this,
            val = that.data.value,
            year = that.data.years[val[0]],
            month = that.data.months[val[1]],
            day = that.data.days[val[2]],
            years2 = [],
            months2 = [],
            days2 = []


        for (let i = year; i <= year + 5; i++) {
            years2.push(i)
        }
        for (let i = month; i <= 12; i++) {
            months2.push(util.formatNumber(i))
        }
        if (year % 4 == 0) {
            if (month == '01' || month == '03' || month == '05' || month == '07' || month == '08' || month == '10' || month == '12') {
                for (var i = day; i <= 31; i++) {
                    days2.push(i)
                }
            } else if (month == '02') {
                for (var i = day; i <= 29; i++) {
                    days2.push(i)
                }
            } else {
                for (var i = day; i <= 30; i++) {
                    days2.push(i)
                }
            }
        } else {
            if (month == '01' || month == '03' || month == '05' || month == '07' || month == '08' || month == '10' || month == '12') {
                for (var i = day; i <= 31; i++) {
                    days2.push(i)
                }
            } else if (month == '02') {
                for (var i = day; i <= 28; i++) {
                    days2.push(i)
                }
            } else {
                for (var i = day; i <= 30; i++) {
                    days2.push(i)
                }
            }
        }

        that.setData({
            chooseTime: false,
            chooseTimeEnd: true,
            sValue: that.data.value,
            years: years2,
            months: months2,
            days: days2,
            days_temp: days2,
            val_temp: val[1],
            value: [0, 0, 0],
        })
    },

    /**
     * 结束时间确定
     */
    confirmEnd() {
        let that = this,
            nav = that.data.nav

        that.data.value[1] = that.data.value[1] + Number(that.data.val_temp)

        that.setData({
            chooseTime: false,
            chooseTimeEnd: false,
            eValue: that.data.value,
            value: [0, date.getMonth(), date.getDate() - 1]
        })
        that.getShop(nav, 0)
    },

    /**
     *  选择开始时间
     */
    bindChange(e) {
        let val = e.detail.value,
            year = this.data.years[val[0]],
            month = this.data.months[val[1]],
            month_temp = this.data.months[0],
            day = this.data.days[val[2]],
            chooseTimeEnd = this.data.chooseTimeEnd

        if (chooseTimeEnd) {
            if (Number(month) > Number(month_temp)) {
                days = []
                if (year % 4 == 0) {
                    if (month == '01' || month == '03' || month == '05' || month == '07' || month == '08' || month == '10' || month == '12') {
                        for (var i = 1; i <= 31; i++) {
                            days.push(i)
                        }
                    } else if (month == '02') {
                        for (var i = 1; i <= 29; i++) {
                            days.push(i)
                        }
                    } else {
                        for (var i = 1; i <= 30; i++) {
                            days.push(i)
                        }
                    }
                } else {
                    if (month == '01' || month == '03' || month == '05' || month == '07' || month == '08' || month == '10' || month == '12') {
                        for (var i = 1; i <= 31; i++) {
                            days.push(i)
                        }
                    } else if (month == '02') {
                        for (var i = 1; i <= 28; i++) {
                            days.push(i)
                        }
                    } else {
                        for (var i = 1; i <= 30; i++) {
                            days.push(i)
                        }
                    }
                }

                this.setData({
                    days: days
                })
            } else {
                this.setData({
                    days: this.data.days_temp
                })
            }
        } else {
            days = []
            if (year % 4 == 0) {
                if (month == '01' || month == '03' || month == '05' || month == '07' || month == '08' || month == '10' || month == '12') {
                    for (var i = 1; i <= 31; i++) {
                        days.push(i)
                    }
                } else if (month == '02') {
                    for (var i = 1; i <= 29; i++) {
                        days.push(i)
                    }
                } else {
                    for (var i = 1; i <= 30; i++) {
                        days.push(i)
                    }
                }
            } else {
                if (month == '01' || month == '03' || month == '05' || month == '07' || month == '08' || month == '10' || month == '12') {
                    for (var i = 1; i <= 31; i++) {
                        days.push(i)
                    }
                } else if (month == '02') {
                    for (var i = 1; i <= 28; i++) {
                        days.push(i)
                    }
                } else {
                    for (var i = 1; i <= 30; i++) {
                        days.push(i)
                    }
                }
            }

            this.setData({
                days: days
            })
        }

        this.setData({
            value: val
        })
    },

    /**
     * 获取数据服务分类
     */
    getShoptype(token) {
        let that = this,
            url = 'Shopdata_getShoptype'
      var token = wx.getStorageSync('token')
        http._post(
            url,
            { token: token },
            res => {
                console.log(res.data)
                let data = res.data
                if (data.re === '1') {
                    that.setData({
                        navList: data.data
                    })
                } else {
                    wx.showToast({
                        title: data.info,
                        icon: 'none',
                        duration: 3000
                    })
                }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 获取分类店铺信息
     */
    getShop(id, p) {
        let that = this,
            token = wx.getStorageSync('token'),
            url = 'Shopdata_getShopdata',
            sVal = that.data.sValue,
            eVal = that.data.eValue,        
            // years = that.data.years,
            // months = that.data.months,
            // days = that.data.days,
          years1 = that.data.years1,
          months1 = that.data.months1,
          days1 = that.data.days1,
        sdate = years1[sVal[0]] + '-' + months1[sVal[1]] + '-' + days1[sVal[2]],
        edate = years1[eVal[0]] + '-' + months1[eVal[1]] + '-' + days1[eVal[2]]
            console.log(that.data),
              console.log(sVal)
            console.log(sdate),  //2018-9-20 
            console.log(edate), //2018-9-20
            console.log(id),  //1
            console.log(p)   //0
            console.log(sVal[0])   //0
            console.log(sVal[1])   //8
            console.log(years[sVal[0]])   //2018
            console.log(months[sVal[1]])  // 09
            console.log(months[0])  //01
        http._post(
            url,
            { token: token, type_id: id, sdate: sdate, edate: edate, page: p },
            res => {
                console.log(res)
                let data = res.data
                if (data.re === '1') {
                    var temp_arr = that.data.dataList
                    // for (let item of data.data.list) {
                    //     temp_arr.push(item)
                    // }
                  var dataList = data.data.list  //直接等于
                  console.log(dataList)
                  console.log(temp_arr)
                    that.setData({
                      dataList: dataList,
                        page: data.data.page,
                        isPage: data.data.is_page,
                        sumPage: data.data.sum_page
                    })
                  console.log(temp_arr)
                  console.log(data.data.page)
                  console.log(data.data.is_page)
                  console.log(data.data.sum_page)
                } else {
                    that.setData({
                        dataList: [],
                        page: data.data.page,
                        isPage: data.data.is_page,
                        sumPage: data.data.sum_page
                    })
                }
                
            }
        )
    },
    //获取AR功能
    lc: function (e) {
      console.log(e)
      wx.showToast({
        title: '暂时没有开通AR',
        icon: 'none',
        duration: 2000
      })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.hideTabBar({
            fail: function () {
                setTimeout(function () {
                    wx.hideTabBar()
                }, 500)
            }
        })
        app.editTabBar()

        let id = app.globalData.id || '1'
        this.setData({
            nav: id
        })
        app.globalData.id = ""

        const token = wx.getStorageSync('token')
        this.getShoptype(token)  // 获取数据服务分类

        let nav = this.data.nav
        this.getShop(nav, 0)  //  获取分类下店铺
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let id = app.globalData.id || '1'
        this.setData({
            nav: id
        })
        app.globalData.id = ""
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let nav = this.data.nav,
            page = this.data.page + 1,
            is_page = this.data.is_page

        if (is_page === '1') {
            this.getShop(nav, page)
        } else {
            wx.showToast({
                title: '没有更多了',
                icon: 'none',
                duration: 3000
            })
        }
    }
})